/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import DTO.LOAINHANVIENDTO;
import java.util.ArrayList;
import DTO.LOAISANPHAMDTO;
import SQL.DataProvider;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Mr_Quay
 */
public class LOAISANPHAMDAO {

    
    public ArrayList<LOAISANPHAMDTO> loaisanphamcoten(String ten)
    {
        DataProvider pro = new DataProvider();
        ArrayList<LOAISANPHAMDTO> ds = new ArrayList<LOAISANPHAMDTO>();
        ResultSet rs=null;
        try
        {
            pro.open();
            String query = "SELECT * FROM LOAISANPHAM WHERE TenLoai LIKE N'%"+ ten + "%'";
            rs = pro.excutequery(query);
            while(rs.next())
            {
                LOAISANPHAMDTO dto = new LOAISANPHAMDTO();
                dto.setMaloaisp(Integer.parseInt(rs.getString("MaLoaiSP")));
                dto.setTenloai(rs.getString("TenLoai"));
                ds.add(dto);
            }
            return ds;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            pro.close();
        }
        return ds;
    }
    
    public LOAISANPHAMDTO loaisanphamcoma(int ma)
    {
        DataProvider pro = new DataProvider();
        ResultSet rs=null;
        LOAISANPHAMDTO dto = new LOAISANPHAMDTO();
        try
        {
            pro.open();
            String query = "SELECT * FROM LOAISANPHAM WHERE MaLoaiSP = " + ma;
            rs = pro.excutequery(query);
            if(rs.next())
            {
                dto.setMaloaisp(Integer.parseInt(rs.getString("MaLoaiSP")));
                dto.setTenloai(rs.getString("TenLoai"));
            }
            return dto;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            pro.close();
        }
        return dto;
    }
    
    public ArrayList<LOAISANPHAMDTO> list_loaisanpham()
    {
        DataProvider pro = new DataProvider();
        ArrayList<LOAISANPHAMDTO> ds = new ArrayList<LOAISANPHAMDTO>();
        ResultSet rs=null;
        try
        {
            pro.open();
            String query = "SELECT * FROM LOAISANPHAM";
            rs = pro.excutequery(query);
            while(rs.next())
            {
                LOAISANPHAMDTO dto = new LOAISANPHAMDTO();
                dto.setMaloaisp(Integer.parseInt(rs.getString("MaLoaiSP")));
                dto.setTenloai(rs.getString("TenLoai"));
                ds.add(dto);
            }
            return ds;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            pro.close();
        }
        return ds;
    }
    public boolean themdanhmuc(LOAISANPHAMDTO dto) 
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "INSERT INTO LoaiSanPham(TenLoai) VALUES(N'"
                    +dto.getTenloai()+"')";
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return false;
    }
    
    public boolean xoadanhmuccoma(int ma)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "DELETE FROM LoaiSanPham WHERE MaLoaiSP = "+ma;
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return false;
    }
    
    
    public boolean xoadanhmuc(LOAISANPHAMDTO dto)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "DELETE FROM LoaiSanPham WHERE MaLoaiSP = "+dto.getMaloaisp();
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean suasanpham(LOAISANPHAMDTO dto)
    {
        DataProvider pro = new DataProvider();
        int kq;
        try
        {
            pro.open();
            String query = "UPDATE LoaiSanPham set "
                    + "TenLoai='"+dto.getTenloai()
                    +"' where MaLoaiSP= "+dto.getMaloaisp();
            kq = pro.excuteupdate(query);
            if(kq>=0)
                return true;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }   
        return false;
    }
}
